package com.openxt.nbzj.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.openxt.nbzj.R;
import com.openxt.nbzj.fragment.OpenxtBaseActivity;
import com.openxt.nbzj.model.ConfigModel;
import com.openxt.nbzj.model.RemotingModel;
import com.openxt.nbzj.remoting.RemotingEx;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import qs.openxt.libs.progress.Wait;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016-07-30.
 */
public class EnterpriseCreditDetailsActivity extends OpenxtBaseActivity implements RemotingEx.OnActionListener {
    @BindView(R.id.NAME)
    TextView NAME;
    @BindView(R.id.ABODE)
    TextView ABODE;
    @BindView(R.id.TEL)
    TextView TEL;
    @BindView(R.id.credit)
    LinearLayout credit;

    Handler mHandler = new Handler();
    Context mContext = null;
    RemotingModel remotingModel = null;
    String ID = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setBaseStyle(R.layout.enterprise_credit_detail_activity, "企业信用详情");
        ButterKnife.bind(this);
        setActionUrl("#/enterpricecredit");
        mContext = this;
        remotingModel = new RemotingModel(mContext);

        Bundle bundle = getIntent().getExtras();
        if(bundle == null)
            return;
        ID = bundle.getString("ID");

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initDetail();
            }
        }, 100);
    }

    private void initDetail(){
        String val = Utils.getLocalStorage(mContext, ConfigModel.ENTERPRISE_GETCREDIT_DETAIL + "$" + ID);
        if(!StringUtil.isNull(val)){
            initDetailView(val);
            remotingModel.doEnterpriseGetcreditDetail(this, false, ID);
        }else{
            Wait.sharedInstance().start(mContext, "正在获取详情..., 请稍候");
            remotingModel.doEnterpriseGetcreditDetail(this, false, ID);
        }
    }

    private void initDetailView(String val){
        if(StringUtil.isNull(val))
            return;
        try{
            JSONObject info = JSON.parseObject(val);
            String $NAME = info.getString("NAME");
            if(StringUtil.isNull($NAME))
                $NAME = "无";
            NAME.setText("\u3000\u3000" + $NAME);

            String $ABODE = info.getString("ABODE");
            if(StringUtil.isNull($ABODE))
                $ABODE = "无";
            ABODE.setText($ABODE);

            String $TEL = info.getString("TEL");
            if(StringUtil.isNull($TEL))
                $TEL = "无";
            TEL.setText($TEL);

            LayoutInflater inflater = LayoutInflater.from(mContext);
            JSONArray $credit = info.getJSONArray("credit");
            if($credit == null)
                $credit= new JSONArray();
            credit.removeAllViews();
            for(int i = 0, j = $credit.size(); i < j; i++){
                JSONObject item = $credit.getJSONObject(i);
                String ENTTYPE = item.getString("ENTTYPE");
                String QUA = item.getString("QUA");
                RelativeLayout item_01 = (RelativeLayout)inflater.inflate(R.layout.credit_item_01, null);
                TextView tv01 = (TextView)item_01.findViewById(R.id.type_name);
                tv01.setText(ENTTYPE + ": " + QUA);
                credit.addView(item_01);

                String SCORE = item.getString("SCORE");
                credit.addView(getItem(inflater, "分值", SCORE));
                String LEVEL = item.getString("LEVEL");
                credit.addView(getItem(inflater, "资质等级", LEVEL));
            }

            credit.invalidate();
        }catch (Exception e){

        }
    }

    private RelativeLayout getItem(LayoutInflater inflater, String arg0, String arg1){
        RelativeLayout item = (RelativeLayout)inflater.inflate(R.layout.credit_item_02, null);
        TextView tv01 = (TextView)item.findViewById(R.id.left_name);
        tv01.setText(arg0);
        TextView tv02 = (TextView)item.findViewById(R.id.right_name);
        tv02.setText(arg1);
        return item;
    }

    @Override
    public void onActionCompleted(String reVal, String actionName) {
        Wait.sharedInstance().stop();
        if(actionName.equals(ConfigModel.ENTERPRISE_GETCREDIT_DETAIL + "$" + ID)){
            Utils.setLocalStorage(mContext, ConfigModel.ENTERPRISE_GETCREDIT_DETAIL + "$" + ID, reVal);
            initDetailView(reVal);
        }
    }

    @Override
    public void onActionFailed(String errorVal, String actionName) {
        Wait.sharedInstance().stopAll();
        showMsg(errorVal);
    }
}
